//
//  SingLeton.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "SingLeton.h"
#import <sqlite3.h>
#import "Movie.h"
@implementation SingLeton

+(instancetype)shareSingleton{
    
    static SingLeton *singleton = nil;
    if (singleton == nil) {
        singleton = [[SingLeton alloc]init];
        
    }
    return singleton;

}

static sqlite3 *db = nil;
static sqlite3 *db1 = nil;
static sqlite3 *db2 = nil;


/*******************************活动数据库*******************************************/
-(void)openDB{
    
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
        
    }
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"Movie.sqlite"];
//    NSLog(@"====%@",sqlPath);
    BOOL result = sqlite3_open(sqlPath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
        
    }
    
}

- (void)createTable
{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS MovieData(numb INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,begin_time TEXT, address TEXT , wisher_count TEXT,category TXT,participant_count TEXT ,image TEXT)";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
        
    }
    
}

- (void)insertStudent:(Movie *)movie{
    
    
//    NSString *str = [movie.begin_time substringWithRange:NSMakeRange(5, 11)];
//    NSString *str1 = [movie.end_time substringWithRange:NSMakeRange(5, 11)];
//    NSString *time = [NSString stringWithFormat:@"%@--%@",str,str1];
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO MovieData(title,begin_time,address,wisher_count,category,participant_count,image)VALUES('%@','%@','%@','%@','%@','%@','%@')",movie.title,movie.begin_time,movie.address,movie.wisher_count,movie.category,movie.participant_count,movie.image];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
//        NSLog(@"插入成功");
    }else{
//        NSLog(@"插入失败");
        
    }
    
    
}



//查询
- (void)selectStudent{
    self.array = [NSMutableArray array];
    NSString *sql = @"SELECT * FROM MovieData ";
    //创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //查询前的准备工作
    //参数3.限制sql语句长度,-1为不限制
    //将数据对象db,sql语句,跟随指针对象stmt关联到一起
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        
        NSLog(@"查询失败");
    }
    //循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //取出第一列数据
        int numb = sqlite3_column_int(stmt, 0);
        //取出第二列数据
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        //取出第三列数据
        const unsigned char *begin_time = sqlite3_column_text(stmt, 2);
        //取出第四列数据
        const unsigned char *address = sqlite3_column_text(stmt , 3);
        const unsigned char *wisher_count = sqlite3_column_text(stmt , 4);
        const unsigned char *category = sqlite3_column_text(stmt , 5);
        const unsigned char *participant_count = sqlite3_column_text(stmt , 6);
        const unsigned char *image = sqlite3_column_text(stmt , 7);
        //将C语言类型转换成OC类型
        NSInteger num = numb;
        NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
        NSString *begin_timeStr = [NSString stringWithUTF8String:(const char *)begin_time];
        NSString *addressStr = [NSString stringWithUTF8String:(const char *)address];
        NSString *wisher_countStr = [NSString stringWithUTF8String:(const char *)wisher_count];
        NSString *categoryStr = [NSString stringWithUTF8String:(const char *)category];
        NSString *participant_countStr = [NSString stringWithUTF8String:(const char *)participant_count];
        NSString *imageStr = [NSString stringWithUTF8String:(const char *)image];
        
//        创建电影对象
        Movie *movie = [[Movie alloc]init];
        movie.title = titleStr;
        movie.begin_time = begin_timeStr;
        movie.address = addressStr;
        movie.wisher_count = wisher_countStr;
        movie.category = categoryStr;
        movie.participant_count = participant_countStr;
        movie.image = imageStr;
        [self.array addObject:movie];
       
        
        
        
    }
    
}

/*********************************用户数据库*****************************************/

-(void)openUserDB
{
    
    if (db1 != nil) {
        NSLog(@"用户数据库已经打开");
        return;
        
    }
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"User.sqlite"];
    NSLog(@"4444====%@",sqlPath);
    BOOL result = sqlite3_open(sqlPath.UTF8String, &db1);
    if (result == SQLITE_OK) {
        NSLog(@"用户打开成功");
    }else{
        NSLog(@"用户打开失败");
        
    }
    
}


- (void)createUserTable
{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS UserData(numbe INTEGER PRIMARY KEY AUTOINCREMENT,UserName TEXT,passWord TEXT, Emile TEXT , contact TEXT)";
    BOOL result = sqlite3_exec(db1, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"用户创建表成功");
        
    }
    
}

- (void)insertUser:(UserModel *)user{
    
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO UserData(UserName,passWord,Emile,contact)VALUES('%@','%@','%@','%@')",user.UserName,user.passWord,user.Emile,user.contact];
    BOOL result = sqlite3_exec(db1, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
        
    }
 
}


- (void)selectUser
{
    self.arrayUser = [NSMutableArray array];
    NSString *sql = @"SELECT * FROM  UserData";
    //创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //查询前的准备工作
    //参数3.限制sql语句长度,-1为不限制
    //将数据对象db,sql语句,跟随指针对象stmt关联到一起
    BOOL result = sqlite3_prepare_v2(db1, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        
        NSLog(@"查询失败");
    }
    //循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        
        //取出第二列数据
        const unsigned char *UserName = sqlite3_column_text(stmt, 1);
        //取出第三列数据
        const unsigned char *passWord = sqlite3_column_text(stmt, 2);
        //将C语言类型转换成OC类型
        NSString *nameStr = [NSString stringWithUTF8String:(const char *)UserName];
        NSString *passWordStr = [NSString stringWithUTF8String:(const char *)passWord];
        //创建对象
        UserModel *user = [[UserModel alloc]init];
       user.UserName = nameStr;
        user.passWord = passWordStr;
        [self.arrayUser addObject:user];
//        NSLog(@"/////////////////////////////%@",self.arrayUser);
        
    }
    
}

/*************************************活动收藏**************************************************/

-(void)openActDB
{
    
    if (db2 != nil) {
        NSLog(@"收藏活动据库已经打开");
        return;
        
    }
     NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"Active.sqlite"];
    NSLog(@"5555====%@",sqlPath);
    BOOL result = sqlite3_open(sqlPath.UTF8String, &db2);
    if (result == SQLITE_OK) {
        NSLog(@"收藏打开成功");
    }else{
        NSLog(@"收藏打开失败");
        
    }
    
}


- (void)createActTable
{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS ActiveData1(numb INTEGER PRIMARY KEY AUTOINCREMENT,UserActName TEXT,MovieName TEXT)";
    BOOL result = sqlite3_exec(db2, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"收藏创建表成功");
        
    }
    
}

- (void)insertAct:(ActiveModel *)actModel{
    
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO ActiveData1(UserActName,MovieName)VALUES('%@','%@')",actModel.UserActName,actModel.MovieName];
    BOOL result = sqlite3_exec(db2, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
        
    }
    
}


- (NSMutableArray *)selectActName:(NSString *)name
{
//    NSString *sql = @"CREATE TABLE IF NOT EXISTS ActiveData1(numb INTEGER PRIMARY KEY AUTOINCREMENT,UserActName TEXT,MovieName TEXT)";

    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat: @"SELECT * FROM ActiveData1 WHERE UserActName = '%@'",name];
    
   
    //创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //查询前的准备工作
    //参数3.限制sql语句长度,-1为不限制
    //将数据对象db,sql语句,跟随指针对象stmt关联到一起
    BOOL result = sqlite3_prepare_v2(db2, sql.UTF8String, -1, &stmt, nil);
    
    NSLog(@"...........=====%d",result);
    if (result == SQLITE_OK) {
        NSLog(@"收藏查询准备成功");
    }else{
        NSLog(@"收藏查询失败");
    }
    
    while (sqlite3_step(stmt) == SQLITE_ROW) {
    
//        const unsigned char *UserActName = sqlite3_column_text(stmt, 1);
      
        const unsigned char *MovieName = sqlite3_column_text(stmt, 2);
        
//        NSString *nameStr = [NSString stringWithUTF8String:(const char *)UserActName];
        NSString *MovieNameStr = [NSString stringWithUTF8String:(const char *)MovieName];
       
//        ActiveModel *act = [[ActiveModel alloc]init];
//        act.UserActName = nameStr;
//        act.MovieName = MovieNameStr;
        
        [array addObject:MovieNameStr];
 
    }
    return array;
    
    
}

-(void)deleteActivityCollect:(NSString *)title userName:(NSString *)userName{
    

    NSString *sql = [NSString stringWithFormat:@"DELETE FROM ActiveData1 WHERE MovieName = '%@' AND UserActName = '%@'",title, userName];
    BOOL result = sqlite3_exec(db2, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }

    
    
    
}













@end
